package com.wsz.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.commonutils.R;
import com.wsz.eduservice.pojo.Teacher;
import com.wsz.eduservice.pojo.vo.TeacherQuery;
import com.wsz.eduservice.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "查询所有讲师")
    @GetMapping("/findAll")
    public R findAllTeacher(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("teachers",list);
    }

    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("/{id}")
    public R removeTeacher(@ApiParam(name="id",value = "讲师id",required = true)
                               @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }
     @ApiOperation(value = "返回当前页讲师")
     @GetMapping({"/pageTeacher/{current}/{limit}"})
     public R pageListTeacher(@ApiParam(name ="current",value = "页码数",required = true)@PathVariable long current,
                              @ApiParam(name = "limit",value = "每页记录数",required = true)@PathVariable long limit){
         Page<Teacher> teacherPage = new Page<>(current,limit);
         teacherService.page(teacherPage, null);
         List<Teacher> records = teacherPage.getRecords();
         long total = teacherPage.getTotal();
         return R.ok().data("total",total).data("records",records);
        }
    @ApiOperation(value = "按条件分页查询讲师")
    @PostMapping({"/pageTeacher/{current}/{limit}"})
    public R likeTeacher(@ApiParam(name ="current",value = "页码数",required = true)@PathVariable long current,
                         @ApiParam(name = "limit",value = "每页记录数",required = true)@PathVariable long limit,
                         @ApiParam(name="teacherQuery",value="查询条件") @RequestBody(required = false) TeacherQuery teacherQuery){
        QueryWrapper queryWrapper = new QueryWrapper();
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if(StringUtils.isNotBlank(name)){
            queryWrapper.like("name",name);
        }
        if(level != null){
            queryWrapper.eq("level",level);
        }
        if(StringUtils.isNotBlank(begin)){
            queryWrapper.ge("gmt_create",begin);
        }
        if(StringUtils.isNotBlank(end)){
            queryWrapper.le("gmt_create",end);
        }
        Page<Teacher> teacherPage = new Page<>(current,limit);

        queryWrapper.orderByDesc("gmt_create");

        teacherService.page(teacherPage, queryWrapper);
        List<Teacher> records = teacherPage.getRecords();
        long total = teacherPage.getTotal();
        return R.ok().data("total",total).data("records",records);
    }

    @ApiOperation("添加讲师")
    @PostMapping("/addTeacher")
    public R addTeacher(@ApiParam(name = "teacher",value = "添加的讲师")@RequestBody(required = false) Teacher teacher){
        boolean save = teacherService.save(teacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation("修改讲师")
    @PostMapping("/updateTeacher")
    public R updateTeacher(@ApiParam(name = "teacher",value = "修改讲师信息")
                           @RequestBody(required = false) Teacher teacher) {
        boolean b = teacherService.updateById(teacher);
        if(b){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @ApiOperation("查询通过id讲师")
    @GetMapping("/getTeacher/{id}")
    public R GetTeacherById(@ApiParam(name = "Id",value = "讲师Id")
                          @PathVariable String id) {
        Teacher byId = teacherService.getById(id);
        return R.ok().data("teacher",byId);
    }
}
